
import json
import base64
from ai.ali.common import *
from urllib import request, parse

class ALIFace():
    def __init__(self, conf):
        print("Use config file ", conf)
        with open(conf, "r") as f:
            account_data = f.read()
            self.account = json.loads(account_data)
            f.close()
    
    def _face_detect_send(self):
        url = self.account["url"]
        tid = self.account["id"]
        tkey = self.account["key"]
        print("url [%s] tid [%s]" % (url, tid))
        content = {}
        content["type"] = 1
        imagestr = str(self.imagebase)
        content["content"] = imagestr[2:len(imagestr)-1]
        content_json = json.dumps(content)
        # print(content_json)
        res = ali_send(url, tid, tkey, content_json)
        print("res: ", res)

    def face_detect(self, imagefile):
        with open(imagefile, "rb") as f:
            imagedata = f.read()
            self.imagebase = base64.b64encode(imagedata)
            f.close()
        self._face_detect_send()
